
angular.module('U9ImHd.utility')

    .factory('Help', ['$ocLazyLoad', 'Config', 'APPCONSTANTS', function ($ocLazyLoad, Config, APPCONSTANTS) {
        var o = {};

        o.getAppFileDir = function () {
            return cordova.file.externalRootDirectory || cordova.file.dataDirectory;
        };
        o.getConfigDir = function () {
            return o.getAppFileDir() + '/' + APPCONSTANTS.appName + '/';
        };
        o.getFullPath = function (src, appId) {
            return getFullPath (src, appId);
        };
        o.getFullURL = function (url) {
            var address = Config.get('address', APPCONSTANTS.defaultAddress),
                port = Config.get('port', APPCONSTANTS.defaultPort);
            if (url[0] !== '\/') {
                url = '\/' + url;
            }
            return 'http://' + address + ':' + port + url;
        };
        o.loadApp = function (appId) {
            return $ocLazyLoad.load({
                name: appId,
                files: [
                    getFullPath('js/main.js', appId),
                    getFullPath('css/main.css', appId)
                ]
            });
        };

        return o;

        function getFullPath (src, appId) {
            var childAppRootDir = '';
            src = appId + '/' + src;
            if (window.cordova && cordova.file) {
                childAppRootDir = o.getConfigDir() + 'apps/';
            } else {
                src = 'wwws/' + src;
            }
            return childAppRootDir + src;
        }
    }]);